package com.zhufeng.web.socket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @ClassName: WebSocketChannelInitializer
 * @Description socket通道初始化
 * @author 月夜烛峰
 * @date 2022/9/23 16:54
 */
public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {
    // 初始化通道
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        // 获取管道，将一个一个的ChannelHandler添加到管道中
        ChannelPipeline pipeline = ch.pipeline();

        // 添加一个http的编解码器
        pipeline.addLast(new HttpServerCodec());
        // 添加一个用于支持大数据流的支持
        pipeline.addLast(new ChunkedWriteHandler());
        // 添加一个聚合器，这个聚合器主要是将HttpMessage聚合成FullHttpRequest/Response
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024));

        // 需要指定接收请求的路由
        // 必须使用以 /nio/info 后缀结尾的url才能访问
        pipeline.addLast(new WebSocketServerProtocolHandler("/nio/info"));
        // 添加自定义的Handler
        pipeline.addLast(new MyScocketHandler());
    }
}

